/****************************************************************************
** Copyright (C) 2017 ZhouLiQiu(zhouliqiu@126.com).
**
** This file is part of the qtc_astyle, It is a plugin for Qt Creator.
**
** Permission is hereby granted, free of charge, to any person obtaining a copy
** of this software and associated documentation files (the "Software"), to deal
** in the Software without restriction, including without limitation the rights
** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
** copies of the Software, and to permit persons to whom the Software is
** furnished to do so, subject to the following conditions:
**
** The above copyright notice and this permission notice shall be included in
** all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
** THE SOFTWARE.
**
****************************************************************************/
#include "cformatprojectthread.h"
#include "cartisticstyle.h"

#include <QFile>
#include <QTextStream>
#include <QTextCodec>

namespace qtc_astyle {
namespace Internal {

CFormatProjectThread::CFormatProjectThread(QObject *parent) :
    QThread(parent)
{
    m_runFlag = false;
}

CFormatProjectThread::~CFormatProjectThread()
{
    stop();
    if (isRunning())
    {
        wait();
    }
}

void CFormatProjectThread::FormatProject(const QStringList &fileList)
{
    stop();
    if (isRunning())
    {
        wait();
    }
    m_fileList.clear();
    m_fileList = fileList;
}

void CFormatProjectThread::stop()
{
    m_runFlag = false;
}

void CFormatProjectThread::run()
{
    m_runFlag = true;
    int fileNum = m_fileList.size();
    int i = 0;

    CArtisticStyle art;
    int result = Format_Result_UnChanged;

    while (m_runFlag && i < fileNum)
    {
        QString fileName = m_fileList.at(i);

        QFile file(fileName);
        if (file.open(QIODevice::ReadOnly))
        {
            QTextStream ts(&file);
            ts.setEncoding(QStringConverter::System);
            QString source = ts.readAll();
            QString formatted;
            bool ret = art.formatString(source, formatted);
            if (!ret)
            {
                result = Format_Result_Failed;
            }
            else
            {
                if (source != formatted)
                {
                    file.close();
                    file.open(QIODevice::WriteOnly | QIODevice::Truncate);
                    ts << formatted;
                    result = Format_Result_Changed;
                }
            }
        }
        else
        {
            qDebug() << "Open file " << fileName << " fail";
        }

        emit FileIsFormatted(i, result);

        i++;
    }
}

} // namespace Internal
} // namespace qtc_astyle
